package alingrad.engine.src.graphics;

import java.io.IOException;

import org.lwjgl.opengl.GL11;

import alingrad.engine.src.Resources;
import alingrad.engine.src.collisions.CollisionMask;

/**
 * Wrapper class for images that can be sure.
 * @author alingrad
 *
 */

public class Sprite implements Renderable {
	/**
	 * Texture associated with the sprite
	 */
	private Texture texture;

	/**
	 * The x location to draw the sprite.
	 */
	public int x;

	/**
	 * The y location to draw the sprite.
	 */
	public int y;

	/**
	 * The width of the texture to draw.
	 */
	public int width;

	/**
	 * The height of the texture to draw.
	 */
	public int height;

	/**
	 * The rotation of the texture to draw.
	 */
	public float rotation;

	/**
	 * The scale of the texture to draw.
	 */
	public float scale;

	/**
	 * Whether the sprite should be mirrored across the x-axis or not.
	 */
	public boolean xmirror;

	/**
	 * Whether the sprite should be mirrored across the y-axis or not.
	 */
	public boolean ymirror;

	/**
	 * The collision mask.
	 */
	private CollisionMask mask;

	/**
	 * Constructs a sprite from a texture already loaded.
	 * @param ref the texture.
	 */
	public Sprite(String ref) {
		texture = Resources.getTexture(ref);

		width = texture.getImageWidth();
		height = texture.getImageHeight();
		rotation = 0;
		scale = 1f;
	}

	/**
	 * Constructs a sprite from a texture loaded from a specific location.
	 * @param ref the location of the image to load from.
	 */
	public Sprite(String ref, boolean collidable) {
		try {
			texture = Resources.getOrLoadTexture(ref, collidable);

			width = texture.getImageWidth();
			height = texture.getImageHeight();
		} catch (IOException e) {
			// a tad abrupt, but our purposes if you can't find a

			// sprite's image you might as well give up.

			System.err.println("Unable to load texture: "+ref);
			System.exit(0);
		}
	}

	/**
	 * Gets the collision mask for the sprite.
	 * @return the collision mask.
	 */
	public CollisionMask getCollisionMask() {
		if (mask == null)
			mask = new CollisionMask(texture.getCollisionMaskData());
		mask.set(x, y, rotation, scale, xmirror, ymirror);
		return mask;
	}

	/**
	 * Draws the sprite.
	 */
	public void draw() {
		GL11.glPushMatrix();

		// bind to the appropriate texture for this sprite

		texture.bind();

		// translate to the right location and prepare to draw
		GL11.glTranslatef(x, y, 0);
		if (xmirror)
			GL11.glScalef(-1.0f, 1.0f, 1.0f);
		if (ymirror)
			GL11.glScalef(1.0f, -1.0f, 1.0f);
		GL11.glRotatef(rotation, 0, 0, 1);
		GL11.glScalef(scale, scale, 1.0f);
    	GL11.glColor3f(1, 1, 1);

		// draw a quad textured to match the sprite

    	GL11.glBegin(GL11.GL_QUADS);
		{
			GL11.glTexCoord2f(0, 0);
			GL11.glVertex2f(-width / 2, -height / 2);
			GL11.glTexCoord2f(0, texture.getHeight());
			GL11.glVertex2f(-width / 2, height / 2);
			GL11.glTexCoord2f(texture.getWidth(), texture.getHeight());
			GL11.glVertex2f(width / 2, height / 2);
			GL11.glTexCoord2f(texture.getWidth(), 0);
			GL11.glVertex2f(width / 2, -height / 2);
		}
		GL11.glEnd();

		// restore the model view matrix to prevent contamination

		GL11.glPopMatrix();
	}
}
